package com.crm.controller.adminController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.ClientdetDAO;
import com.crm.entity.Business;
import com.crm.entity.ClientUser;
import com.crm.entity.Clientdet;

@Controller
@RequestMapping("/client")
public class ClientController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping(value="/open.do")
	public ModelAndView open(HttpSession session){
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		ModelAndView model=new ModelAndView("/jsp/client.jsp");
		model.addObject("submenu", "admin");
		model.addObject("menuName","admin");
		return model;
		
	}
	
	@RequestMapping(value="/save.do",method = RequestMethod.POST)
	public  ModelAndView insert(  HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		String orgName=req.getParameter("orgName");
		String group=req.getParameter("group");
		String division=req.getParameter("div");
		String compShrtName=req.getParameter("compShrtName");
		String tellNumber=req.getParameter("tellNumber");
		String website=req.getParameter("website");
		String fax=req.getParameter("fax");
		
		String emailId=req.getParameter("cliEmail");
		String address=req.getParameter("address");
		System.out.println("Group Is:"+group);
		
		
		
		
		String ctype=req.getParameter("ctype");
		System.out.println("Customer Type Is:"+ctype);
		String rcount=req.getParameter("rowcount");
		int rowcount=Integer.parseInt(rcount);
		logger.info("row count "+rowcount);
		Clientdet cld=new Clientdet(bus,orgName,compShrtName,website,address,division,tellNumber,emailId,fax,false,group,ctype);
		if(req.getParameter("id")!=null){
			int cliId=0;
			cliId=Integer.parseInt(req.getParameter("id"));
			cld.setId(cliId);
			 System.out.println("Updated Client Details");
		}else{
			System.out.println("Created New Client Details");
		}
		List<ClientUser> cliuser=new ArrayList<ClientUser>();
		for(int i=1;i<=rowcount;i++){
			String userName=req.getParameter("userName"+i);
			String designation=req.getParameter("designation"+i);
			String div=req.getParameter("division"+i);
			String phone=req.getParameter("phone"+i);
			String cellPhone=req.getParameter("cell"+i);
			String userfax=req.getParameter("fax"+i);
			String userGroup=req.getParameter("userGroup"+i);
			System.out.println("User Group Is:"+userGroup);
			String email=req.getParameter("email"+i);
			ClientUser clu=new ClientUser(bus,userName,designation,div,phone,email,userGroup,cellPhone,userfax);
			if(req.getParameter("userId"+i)!=null){
				clu.setId(Integer.parseInt(req.getParameter("userId"+i)));
			}
			cliuser.add(clu);
		}
		cld.setCliuser(cliuser);
		System.out.println(cld);
		ClientdetDAO cd =new  ClientdetDAO();
		try{
			cd.insert(cld);
			model.addObject("clientdet","Successfully Created Client Details");
		}catch(Exception e){
			e.printStackTrace();
			model.addObject("clientdet","Failed To Create Client Details");
		}
		model.setViewName("redirect:open.do");
		return model;
		
	}
	@RequestMapping("/cliNameEditCheck.do")
	public @ResponseBody String busNameEditCheck(@RequestParam(value="name") String name,@RequestParam(value="cliid")String id,HttpSession session)throws IOException{
		Business bus=(Business)session.getAttribute("business");
		int busId=bus.getBusinessId();
		logger.info("the business id is"+id);
		int idno=Integer.parseInt(id.trim());
		String b="false";
		ClientdetDAO cDAO=new ClientdetDAO();
		try {
			if(idno!=0)
			{
				b=cDAO.clientCheckwithID(busId,name.trim(),idno);	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
		
	}
	@RequestMapping("/cliNameCheck.do")
	public @ResponseBody String busNameCheck(@RequestParam(value="name") String name,HttpSession session)throws IOException{
		
		Business bus=(Business)session.getAttribute("business");
		int busId=bus.getBusinessId();
	
		String b="false";
		ClientdetDAO cDAO=new ClientdetDAO();
		try {
			b=cDAO.clientCheck(busId,name.trim());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
		
	}
	@RequestMapping(value="/getClientDet.do",method = RequestMethod.GET)
	public @ResponseBody
	String getClientDet(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		ClientdetDAO cd =new  ClientdetDAO();
		StringBuilder sb=new StringBuilder();
		List<Clientdet> c=cd.getAllCli(bus.getBusinessId());
		Iterator<Clientdet> itr=c.iterator();
		while(itr.hasNext()==true){
			Object ob=itr.next();
			Clientdet cli=(Clientdet)ob;
			sb.append(cli.getOrganisation());
			sb.append(",");
		}
		return sb.toString();
	}
	@RequestMapping(value="/clientEdit.do",method = RequestMethod.POST)
	public  ModelAndView clientEdit(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		String cliName=req.getParameter("clientName");
		
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		ClientdetDAO cd =new  ClientdetDAO();
		Clientdet clidet=cd.getClient(bus.getBusinessId(),cliName);
		List<ClientUser> cliUserLst=cd.getUser(bus.getBusinessId(),clidet.getId());
		model.setViewName("/jsp/editClient.jsp");
		model.addObject("clidet",clidet);
		model.addObject("cliUserlst",cliUserLst);
		model.addObject("submenu","admin");
		model.addObject("menuName","admin");
		return model;
	}
}
